﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YXQ.Dao.ViewModel;
using AutoMapper;
namespace YXQ.Dao
{

    public class MessageDao
    {


        public MessageDao()
        {

            Mapper.CreateMap<Y_Message, MessageModel>();
        }
        public List<Y_Message> GetList(int startIndex, int maxRow, out int recordcount)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                recordcount = edm.Y_Message.Count();

                return edm.Y_Message.OrderByDescending(s => s.Id).Skip(startIndex).Take(maxRow).ToList();

            }

        }


        public MessageModel GetOneById(int id)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                var query = edm.Y_Message.FirstOrDefault(s => s.Id == id);
                return Mapper.Map<Y_Message, MessageModel>(query);
            }

        }

        public List<MessageModel> GetListModel(string stwhr, int startIndex, int max, out int reco)
        {
            using (YXQManageEntities edm = new YXQManageEntities())
            {
                var query = from s in edm.Y_Message
                          
                            select s;
                if (!string.IsNullOrEmpty(stwhr)) {
                    query = query.Where(s=>s.qname.IndexOf(stwhr)>0);
                }
                reco = query.Count();
                return Mapper.Map<List<Y_Message>, List<MessageModel>>(query.OrderByDescending(s => s.Id).Skip(startIndex).Take(max).ToList()); ;
            }
        }


        public int Save(MessageModel obj)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                var query = edm.Y_Message.FirstOrDefault(s => s.Id == obj.Id);

                if (query == null)
                {


                    query = new Y_Message();
                    query.createtime = obj.createtime;
                    query.createuserid = obj.createuserid;

                    query.createusername = obj.createusername;
                    edm.AddToY_Message(query);

                }


                query.qname = obj.qname;

                query.qcontent = obj.qcontent;

                query.touser = obj.touser;

                query.isslave = obj.isslave;

                query.slavetime = obj.slavetime;

                edm.SaveChanges();

                return query.Id;


            }

        }


        public int DelById(int id)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                var query = edm.Y_Message.FirstOrDefault(s => s.Id == id);

                if (query != null)
                {


                    edm.DeleteObject(query);


                }


                return edm.SaveChanges();


            }

        }


    }

}

